package example.players;

import java.util.Random;

import rafael.bot.Model;

public class ExpectedAnswerModel extends Model {
	private static final double MAX_TRUST = 0.999;
	private double pMove[];
	private Random random;

	public ExpectedAnswerModel() {
		pMove = new double[4];//probabilidade das jogadas
		random = new Random();
	}

	
	@Override
	public void learn(int[] input, int output,double learningRate) {
		if(pMove[output] < MAX_TRUST) {
			pMove[output] *= 2;
		}
		normalize();
	}

	@Override
	public int predictMove(int[] input) {
		int move = 0;
		double u = random.nextDouble();
		double ac = 0;
		for(int i = 1; i <= 3; i++) {
			ac += pMove[i];
			if(u < ac) {
				move = i;
				break;
			}
		}
		return move; 
	}

	@Override
	public void init(int moves) {
		// inicia probabilidade c/ distr. uniforme
		for (int i = 1; i < pMove.length; i++) {
			pMove[i] = 1.0 / 3.0;
		}
	}
	
	private void normalize() {
		double size = 0;
		for(int i = 0; i < pMove.length; i++) {
			size += pMove[i];
		}
		for(int i = 1; i < pMove.length; i++) {
			pMove[i] /= size;
		}
	}

}
